﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Data.SqlClient;
using DevExpress.Web.ASPxTreeList;

public partial class UserControl_ControlPermissionOfUserViewData : System.Web.UI.UserControl
{
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    ClassExecute.ClassExecute dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //GetTuyen();
            ////nằm trong thread
            ThreadStart ths2 = new ThreadStart(GetUserType);
            Thread th2 = new Thread(ths2);
            th2.Start();

            th2.Join();
            ////nằm trong thread

        }
        GetTuyen();
        MessageBox.ShowOnPageLoad = false;
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGridSearch();
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        if(lblUserName.Text !="")
        {

             if (dt.ExecuteSQl_bool(str, CommandType.StoredProcedure,"TTr_Users_UpdateAdnCodeViewData", ref error,
                    new SqlParameter("@UserName", lblUserName.Text),
                    new SqlParameter("@ADNCode", treeListTuyen.FocusedNode.Key)))
            {
                ShowMessageBox("Cập nhật thành công");
            }
            else
            {
                ShowMessageBox("Cập nhật thất bại");
            }

        }
        else       
        {
            ShowMessageBox("Bạn hãy chọn user cần phân quyền xem dữ liệu");
        }
    }

    void GetTuyen()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_GetTuyenViewData", CommandType.StoredProcedure);
        treeListTuyen.DataSource = db;
        treeListTuyen.DataBind();        
        treeListTuyen.ExpandToLevel(1);
    }    
    
    void GetUserType()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "TTr_getUserTypeList", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlThuocNhom, "TypeID", "TypeName");
    }
    void BindGridSearch()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[TTr_FindUserSystem]", CommandType.StoredProcedure,
            new SqlParameter("@parameter", txtSearch.Text),
            new SqlParameter("@TypeID", ddlThuocNhom.SelectedValue));

        if (db.Rows.Count > 0)
        {
            grvUser.DataSource = db;
        }
        else
        {
            grvUser.DataSource = null;
        }
        grvUser.DataBind();

    }
    protected void grvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvUser.PageIndex = e.NewPageIndex;
        BindGridSearch();

    }
    protected void grvUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            if (e.CommandName == "chon")
            {

                GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                Label lblChiNhanhgv = (Label)grv.FindControl("lblSiteName");
                Label lblUsergv = (Label)grv.FindControl("lblUser");
                Label lblDescriptiongv = (Label)grv.FindControl("lblDescription");
                Label lblGruopgv = (Label)grv.FindControl("lblgroup");
                Label lblADNCode = (Label)grv.FindControl("lblADNCode");
                lblChiNhanh.Text = lblChiNhanhgv.Text;
                lblUserName.Text = lblUsergv.Text;
                lblDescription.Text = lblDescriptiongv.Text;
                lblGroup.Text = lblGruopgv.Text;
                try
                {
                    if (lblADNCode.Text != "")
                    {
                        treeListTuyen.FindNodeByKeyValue(lblADNCode.Text).Focus();
                        treeListTuyen.FindNodeByKeyValue(lblADNCode.Text).Expanded = true;
                    }

                }
                catch
                { 
                
                }

                grvUser.DataSource = null;
                grvUser.DataBind();

                TabContainer1.ActiveTabIndex = 0;
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
}
